/*
 * communicate_uart.c
 *
 *  Created on: 2022.2.26
 *      Author: huan
 */
#include <rtthread.h>
#include "uart_dma.h"



void vcom_rxTask(void *parameter)
{
    uint8_t size_dat = 0;
    char dat_p[64] = {0};
    while (1)
    {
        size_dat = USER_VCOM.recv(dat_p, RT_WAITING_FOREVER);
        rt_kprintf("recv %d\r\n", size_dat);
        USER_VCOM.send(dat_p, size_dat);
    }
}
void vcom_txTask(void *parameter)
{
    while (1)
    {
        USER_VCOM.send("hello", sizeof("hello"));
        rt_thread_mdelay(1000);
    }
}
/*-------------------------------------------------------------------------------------------------*/
#ifdef BSP_USING_UART4
void uart4_rxTask(void* parameter)
{
    size_t size_dat = 0;
    char dat_p[200] =
    {   0};
    while(1)
    {
        size_dat = USER_UART4.recv(dat_p, RT_WAITING_FOREVER);
        if(size_dat)
        {
            USER_UART4.send("I receive data!\r\n",sizeof("I receive data!\r\n"));
        }
    }
}
void uart4_txTask(void* parameter)
{
    while(1)
    {
        USER_UART4.send("uart4 is sending!\r\n",sizeof("uart4 is sending!\r\n"));
        rt_thread_mdelay(1000);
    }
}
#endif


#ifdef BSP_USING_UART3
void uart3_rxTask(void* parameter)
{
    size_t size_dat = 0;
    uint8_t dat_p[TX_BUFFER+4] = { 0 };
    while (1)
    {
        size_dat = USER_UART3.recv((char*)dat_p, RT_WAITING_FOREVER);
        if((dat_p[0] == 0xAA) && (dat_p[1] == 0xAF))
        {
            switch (dat_p[2])
            {
            case 0:
                Screen_Handle.value1 = 0;
                break;
            case ((0x01 << 5) | 0x01):
                Screen_Handle.value1 = 1;
                break;
            case ((0x02 << 5) | 0x02):
                Screen_Handle.value1 = 2;
                break;
            case ((0x03 << 5) | 0x03):
                Screen_Handle.value1 = 3;
                break;
            case ((0x03 << 5) | 0x13):
                Screen_Handle.value1 = 4;
                break;
            case ((0x05 << 5) | 0x05):
                Screen_Handle.value1 = 5;
                break;
            case ((0x06 << 5) | 0x06):
                Screen_Handle.value1 = 6;
                break;
            case ((0x06 << 5) | 0x16):
                Screen_Handle.value1 = 7;
                break;
            default:
//                    rt_kprintf("read %x\n", dat_p[2]);
                Screen_Handle.value1 = 0xff;
                break;
            }

            if (dat_p[2] == ((0x06 << 5) | 0x16))
            {
                uint8_t i = 0;
                while (dat_p[3+i] != '\0')
                {
                    Screen_Handle.car_plate[i] = dat_p[3+i];
                    i++;
                }
                Screen_Handle.car_plate[i] = '\0';
            }
        }
    }
}
void uart3_txTask(void* parameter)
{
    rt_thread_mdelay(1000); // 等 screen 线程启动

    char data_to_send[3] = {0xAA, 0xAF, 0};
    while (1)
    {
        rt_sem_take(Screen_Handle.sem, RT_WAITING_FOREVER);
        data_to_send[2] = Screen_Handle.Tcmd;
        USER_UART3.send(data_to_send, 3);
    }
}
#endif
/*-------------------------------------------------------------------------------------------------*/

static int communicate_init(void)
{
    return 0;
}
INIT_APP_EXPORT(communicate_init);
/*-------------------------------------------------------------------------------------------------*/
